package com.moetube.user.service.impl;

import com.moetube.execption.ContinueSignException;
import com.moetube.user.dao.SignMapper;
import com.moetube.user.dao.UserMapper;
import com.moetube.user.pojo.Sign;
import com.moetube.user.pojo.User;
import com.moetube.user.service.UserService;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public User findByUserId(String id){
        return  userMapper.selectByPrimaryKey(id);
    }


    public User findByUsername(String username){
        return userMapper.findByUsername(username);
    }


    /**
     * 增加
     * @param user
     */
    @Override
    public void add(User user){
        userMapper.insert(user);
    }


    /**
     * 修改
     * @param user
     */
    @Override
    public void update(User user){
        userMapper.updateByPrimaryKey(user);
    }



    @Autowired
    private SignMapper signMapper;

    /**
     * 连续签到功能
     * @throws ContinueSignException
     */
    @Override
    @Transactional
    public void sign() throws ContinueSignException {
        //获取签到用户
        String username = "lisi";//模拟
        //获取签到时间
        Date date = new Date();
        //查询签到表 -》有记录/无记录
        Sign sign = signMapper.findSignByUsername(username);

        //无记录 添加
        if ( sign == null ) {
            //无记录新增一条记录 时间count+1 continue_sign +1
            Sign sign1 = new Sign();
            //sign1.setId("1");
            sign1.setUsername(username);
            sign1.setCreateTime(date);//获取签到时间
            sign1.setContinueSign(1);
            sign1.setCount(1);
            sign1.setUpdateTime(date);
            signMapper.insertSelective(sign1);
            return;
        }

        //有记录 判断是不是连续签到
        //查询 跟新时间 是不是今天
        Date update = signMapper.findDateByUsername(username);

        //引入 org.apache.commons.lang.time.DateUtils
        boolean sameDay = DateUtils.isSameDay(date, update);
        //同一天
        if ( sameDay ) {
            //同一天连续签到 抛出异常错误
            throw new ContinueSignException("重复签到");
        }
        //不是同一天
        //判断和上次跟新时间间隔大于1天没有
        //获取毫秒值来相减
        long dateTime = date.getTime();
        long updateTime = update.getTime();
        long continueTime = dateTime - updateTime;
        //2天的毫秒值
        long oneTime = 60 * 60 * 24 * 1000 * 2 ;
        //判断是不是连续签到
        // 不是连续签到
        if ( continueTime > oneTime ) {
            //间隔大于一天 连续签到 变成一
            //总数加一
            Integer count = sign.getCount() + 1;
            sign.setCount(count);
            sign.setUpdateTime(new Date());
            sign.setContinueSign(1);
            signMapper.updateByPrimaryKeySelective(sign);
            return;
        }
        //是连续签到
        int continueSign = sign.getContinueSign();
        if ( continueSign == 6 ) {
            //总数加一
            Integer count = sign.getCount() + 1;
            sign.setCount(count);
            // 正好 为6 清空 币多加100
            sign.setContinueSign(0);
            sign.setUpdateTime(new Date());
            signMapper.updateByPrimaryKeySelective(sign);
        } else {
            //总数加一
            Integer count = sign.getCount() + 1;
            sign.setCount(count);
            // 不为 6   ContinueSign+1
            sign.setContinueSign(sign.getContinueSign() + 1);
            sign.setUpdateTime(new Date());
            signMapper.updateByPrimaryKeySelective(sign);
        }
    }

}
